package com.ruiyian.project.road.controller;

import com.ruiyian.common.utils.SecurityUtils;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.project.road.domain.Road;
import com.ruiyian.project.road.service.RoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/19
 * Desc: 道路控制层
 */
@Slf4j
@RestController
@RequestMapping("/road/info")
public class RoadController extends BaseController {
    @Autowired
    private RoadService roadService;


    /**
     * 查询列表
     * @param road
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Road road) {
        startPage();
        List<Road> list = roadService.selectList(road);
        return getDataTable(list);
    }

    /**
     * 道路列表下拉
     * @param road
     * @return
     */
    @PostMapping("/listAll")
    public AjaxResult listAll(@RequestBody Road road) {
        List<Road> list = null;
        try {
            list = roadService.selectList(road);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('road:rd:remove')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            roadService.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @PreAuthorize("@ss.hasPermi('road:rd:remove')")
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody List<Road> list) {
        try {
            roadService.deleteBatch(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */

    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Road road = null;
        try {
            road =  roadService.findOne(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(road);
    }

    /**
     * 保存
     * @param road
     * @return
     */
    @PreAuthorize("@ss.hasPermi('road:rd:edit')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Road road) {
        try {
            if(road.getId()!=null){
                road.setUpdateBy(SecurityUtils.getUsername());
                roadService.edit(road);
            }else {
                road.setCreateBy(SecurityUtils.getUsername());
                roadService.save(road);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(road);
    }
}
